/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

/**
 *
 * @author Administrator
 */
import SQL.SQL;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public final class getInterest {

    public static Map<String, Integer> interestMap = new HashMap<String, Integer>();
    public static Map<String, Double> interestRate = new HashMap<String, Double>();
    public static Map<String, String> friendModule = new HashMap<String, String>();
    public static int fameCount = 0;

    public static void getInterest(DataPreparation data) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        getLargeCatInterest(data);
        getInterestRate();
        saveSQL();
    }

    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Auth.Auth4Code.prepareWeibo();
        DataPreparation data = new DataPreparation();
        getInterest(data);
    }

    public static void getLargeCatInterest(DataPreparation data) {
        for (String u : data.data.tagFameLarge) {
            interestMap.put(u, 0);
        }
        for (String u : data.data.mFIDs) {
            if (data.data.fameIDandTag.containsKey(u)) {
                fameCount += 1;
                String tag = data.data.fameIDandTag.get(u).split("/")[0];
                friendModule.put(u, tag);
                int newCount = interestMap.get(tag) + 1;
                interestMap.put(tag, newCount);
            }
        }
    }

    public static void getInterestRate() {
        Set keys = interestMap.keySet();
        int count = keys.size();
        for (int i = 0; i < count; i++) {
            String key = (String) keys.toArray()[i];
            int interestCount = interestMap.get(key);
            if (interestCount > 0) {
                interestRate.put(key, (double) interestCount / (double) fameCount);
            }
        }
    }

    private static void saveSQL() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        String text = "";
        Set keys = interestRate.keySet();
        int count = keys.size();
        List<String> sortInterestRate = new ArrayList<String>();
        for (int i = 0; i < count; i++) {
            String key = (String) keys.toArray()[i];
            Double value = interestRate.get(key);
            String vk = value.toString() + "\t" + key;
            sortInterestRate.add(vk);
        }
        Collections.sort(sortInterestRate);
        
        int size = 21;
        if (count < 10) {
            size = 2*count + 1;
        }
        String[] column = new String[size];
        String[] value = new String[size];
        column[0] = "uid";
        value[0] = Config.uid;
        for (int i = 0; i < count; i++) {
            if(i>9)break;
            String s[] = sortInterestRate.get(count - i-1).split("\t");
            column[2*i+1]="interest"+(i+1);
            column[2*(i+1)]="rate"+(i+1);
            value[2*i+1]=s[1];
            value[2*(i+1)]=s[0];
            text+=s[1]+"|"+s[0]+"/";
        }
        String table = "wb_demo_interest";
        String[] column2={"interest"};
        String[] value2={text};
        SQL.SQL(SQL.updateValue(column2, value2, "wb_demo_user", "uid", Config.uid));
        SQL.SQL(SQL.insert(column, value, table));
        
    }
}
